package com.csun.cmny.provider.controller;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dto.OmcMenuDto;
import com.csun.cmny.provider.model.domain.OmcMenu;
import com.csun.cmny.provider.model.vo.OmcMenuVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcMenuService;
import com.csun.cmny.provider.utils.MenuTimeUtil;
import com.csun.cmny.provider.utils.NursinglogUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;


@RestController
@RequestMapping(value = "/menu", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcMenuController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcMenuService omcMenuService;


    /**
     * 添加一周菜谱
     */
    @RequestMapping(value = "/menuAdd", method = RequestMethod.POST)
    public Wrapper foodAdd(String day) {
        String pattern = "^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])"
                + "|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|"
                + "[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$";
        //验证日期格式
        if (StringUtils.isNotBlank(day) && !Pattern.matches(pattern, day)) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("Advisory.date.pattem.error", null), null);
        }
        OmcMenu omcMenu = new OmcMenu();
        omcMenu.setGroupId(getLoginAuthDto().getGroupId());
        omcMenu.setDay(day);
        int n = omcMenuService.selectCount(omcMenu);
        if (n > 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("Menu.record.exist", null), n);
        }

        List<OmcMenu> menuList = new ArrayList<OmcMenu>();

        List<String> list = MenuTimeUtil.getDays(day);
        Integer week = Integer.valueOf(list.get(1) + list.get(0));
        //将传入日期一周内的菜谱插入菜谱表
        for (int i = 2; i <= 8; i++) {
            OmcMenu menu1 = new OmcMenu();
            menu1.setGroupId(getLoginAuthDto().getGroupId());
            menu1.setWeek(week);
            menu1.setDay(list.get(i));
            menu1.setUpdateInfo(getLoginAuthDto());
            menuList.add(menu1);
        }

        int m = omcMenuService.batchSave(menuList);
        if (m <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("Menu.add.fail", null), m);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("Menu.add.success", null), m);
    }

    /**
     * 根据时间判断该周菜谱是否已经存在
     */
    @RequestMapping(value = "/selectByDay", method = RequestMethod.POST)
    public Wrapper selectByDay(String day) {
        String pattern = "^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])"
                + "|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|"
                + "[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$";
        //验证日期格式
        if (StringUtils.isNotBlank(day) && !Pattern.matches(pattern, day)) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("Advisory.date.pattem.error", null), null);
        }
        OmcMenu omcMenu = new OmcMenu();
        omcMenu.setGroupId(getLoginAuthDto().getGroupId());
        omcMenu.setDay(day);
        int n = omcMenuService.selectCount(omcMenu);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("Menu.week.record.no.exist", null), n);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("Menu.week.record.exist", null), n);
    }


    /**
     * 修改每周菜谱
     */
    @RequestMapping(value = "/menuUpdate", method = RequestMethod.POST)
    public Wrapper menuUpdate(@RequestBody OmcMenuDto omcMenuDto) {
        if (omcMenuDto.getMenus() == null || omcMenuDto.getMenus().size() <= 0) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("Menu.update.param.error", null), null);
        }
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        List<OmcMenu> menus = omcMenuDto.getMenus();
        //验证菜谱参数
        for (OmcMenu omcMenu : menus) {
            String breakfast = omcMenu.getBreakfast();
            if (breakfast != null && breakfast.length() > 128) {
                return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("Menu.breakfast.param.message", null), null);
            }
            String lunch = omcMenu.getLunch();
            if (lunch != null && lunch.length() > 128) {
                return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("Menu.lunch.param.message", null), null);
            }
            String dinner = omcMenu.getDinner();
            if (dinner != null && dinner.length() > 128) {
                return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("Menu.dinner.param.message", null), null);
            }
            omcMenu.setUpdateInfo(loginAuthDto);
        }
        //批量修改菜谱
        int n = omcMenuService.batchUpdate(menus);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("Menu.update.fail", null), n);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("Menu.update.success", null), n);
    }

    /**
     * 查询周菜谱
     */
    @RequestMapping(value = "/menuShowAll", method = RequestMethod.POST)
    public Wrapper menuShowAll(String day) {
        String pattern = "^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])"
                + "|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|"
                + "[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$";
        //验证日期格式
        if (StringUtils.isNotBlank(day) && !Pattern.matches(pattern, day)) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, cmnyMessageSource.getMessage("Advisory.date.pattem.error", null), null);
        }
        //组装参数
        OmcMenu omcMenu = new OmcMenu();
        omcMenu.setGroupId(getLoginAuthDto().getGroupId());
        omcMenu.setDay(day);
        List<OmcMenuVo> list = omcMenuService.selectByDayAll(omcMenu);
        if (list.size() <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource.getMessage("Menu.query.fail", null), list);
        }
        for(int i=0;i<=list.size()-1;i++){
            OmcMenuVo menu1 = list.get(i);
            //获取早餐食物名称集合
            if(!(menu1.getBreakfast() == null || menu1.getBreakfast().length() <= 0)){
                menu1.setBreakfasts(NursinglogUtil.getFoodNameList(menu1.getBreakfast()));
            }
            //获取中餐食物名称集合
            if(!(menu1.getLunch() == null || menu1.getLunch().length() <= 0)){
                menu1.setLunchs(NursinglogUtil.getFoodNameList(menu1.getLunch()));
            }
            //获取晚餐食物名称集合
            if(!(menu1.getDinner() == null || menu1.getDinner().length() <= 0)){
                menu1.setDinners(NursinglogUtil.getFoodNameList(menu1.getDinner()));
            }
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("Menu.week.record.exist", null), list);
    }
}
